<script setup lang="ts">
import { Form } from '@/components/Form'
import { useForm } from '@/hooks/web/useForm'
import { PropType, reactive, ref,watch } from 'vue'
import { ViewData } from '@/api/types/userType'
import { useValidator } from '@/hooks/web/useValidator'
import MonacoEditor from '@/components/MonacoEditor/index.vue'
const { required} = useValidator()

const props = defineProps({
  currentRow: {
    type: Object as PropType<Nullable<ViewData>>,
    default: () => null
  },
  formSchema: {
    type: Array as PropType<FormSchema[]>,
    default: () => []
  }
})
const { register, methods, elFormRef } = useForm({schema: props.formSchema});
const { setValues } = methods;
const rules = reactive({
  courseId: [required()],
  type: [required()]
})

const script=ref("")
const MonacoEditRef=ref<InstanceType<typeof MonacoEditor>>()
watch(()=>script,(script)=>{
  setValues({script:script.value});
},{
    deep: true,
    immediate: true
  })
watch(()=>props.currentRow,(currentRow)=>{
  if(!currentRow) return;
  script.value=currentRow.script;
  setValues(currentRow) ;
  elFormRef;
},{
    deep: true,
    immediate: true
  })
const setformData=(value)=>{
  setValues({...value});
}
defineExpose({
  elFormRef,
  getFormData: methods.getFormData,
  setFormData:setformData
})
</script>

<template>
  <Form :rules="rules" @register="register" />
  <div style="height: 350px;border: 1px solid #efefef;">
    <MonacoEditor ref="MonacoEditRef" v-model="script" language="yaml" />
  </div>
</template>
